/**
 * @fileoverview Giant Interective Group, Inc. Javascript Library v#version.
 * 该Javascript UI库是基于jQuery的扩展。
 * 使用方式：
 * <pre>
 * Copyright (C) 2004-2009 Giant Interective Group, Inc. All rights reserved.
 * 版权所有 2004-2009 上海巨人网络科技有限公司
 * </pre>
 * @version #version, #date
 * @author  Zhangkai
 * Depend on jQuery 1.3.x
 */

 (function($) {
 	 /** @namespace Giant UI 库核心名称空间。*/
    if (giant.ui == null || giant.ui == 'undefined') {
        giant.ui = {};
    }
    var isOpen = false;
    /**
     * 创建个新的giant.ui.tip。
     * @giant.ui.tip tip提示组件。
     * @constructor
     * @returns {giant.ui.tip} 
     * @example
     * <pre>
     *   var tip2 = new giant.ui.tip({
     *   tipId:"#tip",
     *   eventTarget:"input",
     *   tipContent:"ajax",
     *   openTrigger:"click",
     *   closeTrigger:".giant-ui-tip-close",
     *   ajax:{
     *       url:"ajax.html",
     *       type:"get",
     *       loadText:"数据加载中。。。"
     *   }
     *   });
     * </pre>
     */
    giant.ui.tip = function(options) {
        var opts = $.extend({},
        giant.ui.tip.defaults, options);
        _init();
        _openTipBind();
        _closeTipBind();
        function _init() {
            if (!$(opts.tipId).length) {
                $("body").append("<div id=\"" + opts.tipId.replace("#", "") + "\"></div>");
                $(opts.tipId).css("z-index", opts.zindex);
                if (opts.width != "auto") {
                    $(opts.tipId).css("width", opts.width + "px");
                }
                if (opts.height != "auto") {
                    $(opts.tipId).css("height", opts.height + "px");
                    $(opts.tipId).css("overflow-y", "auto");
                }
            }
        }
        //tip显示事件绑定
        function _openTipBind() {
            if (opts.openTrigger == "mouseover") {
                $(opts.eventTarget).bind("mouseover", _openEvent);
            } else if (opts.openTrigger == "click") {
                $(opts.eventTarget).bind("click", _openEvent);
            }
        }
        //tip关闭事件绑定
        function _closeTipBind() {
            if (opts.closeTrigger == "mouseout") {
                $(opts.eventTarget).bind("mouseout", _closeEvent);
            } else {
                $(opts.closeTrigger).bind("click", _closeEvent);
            }
        }
        function _openEvent(event) {
            if (!isOpen) {
                var position = {
                    x: 0,
                    y: 0
                };
                if (opts.tipContent != "ajax") {
                    $(opts.tipId).text($(this).attr(opts.tipContent));
                } else {
                    $(opts.tipId).html(opts.ajax.loadText);
                    $.ajax({
                        type: opts.ajax.type,
                        url: opts.ajax.url,
                        data: opts.ajax.data,
                        success: function(msg) {
                                $(opts.tipId).html(msg);
                                _closeTipBind();
                            }
                        });
                }
                var event = event || window.event;
                if (opts.position == "mouse") {
                    position = _getMousePosition(event);
                    position = _calculatePosition(position, $(opts.tipId).width(), $(opts.tipId).height());
                    $(opts.tipId).css({
                        "left": position.x + "px",
                        "top": position.y + "px"
                    });
                } else {

                }
                _showTip();
                isOpen = true;
            }
        }
        //tip的展示方式
        function _showTip() {
            switch (opts.showType) {
            case "show":
                $(opts.tipId).show();
                break;
            case "fadeIn":
                $(opts.tipId).fadeIn(opts.showSpeed);
                break;
            case "slideDown":
                $(opts.tipId).slideDown(opts.showSpeed);
                break;
            }
        }
        function _closeEvent(event) {
            $(opts.tipId).hide();
            isOpen = false;
        }
        //计算tip弹出的位置
        function _calculatePosition(position, width, height) {
            var winWidth = $("body").width();
            var winHeight = $(document).height();
            if (position.x + width > winWidth) {
                position.x = winWidth - width - 10;
            }
            if (position.y + height > winHeight) {
                position.y = position.y - height - 30;
            }
            return position;
        }
        function _getDomPosition(dom) {
            return {
                x: $(dom).position().left,
                y: $(dom).position().top
            };
        }
        //获取鼠标事件
        function _getMousePosition(event) {
            if (window.event) { //ie
                return {
                    x: event.clientX + document.documentElement.scrollLeft + 10,
                    y: event.clientY + document.documentElement.scrollTop + 10
                };
            } else { //DOM 标准浏览器
                return {
                    x: event.pageX + 10,
                    y: event.pageY + 10
                };
            }
        }
        function _cancelBuble(event) {
            if (event && event.stopPropagation) {//DOM 标准浏览器
                event.stopPropagation();
            }
            else{ //IE
                window.event.cancelBubble = true;
            }
        }
    };
    /**
     * @default width==>tip宽度,设为auto则宽度自适应<br />
     *          height==> tip高度,设为auto则高度自适应<br />
     *          tipId==> tip框的id,带#<br />
     *          zIndex==> tip 的css中zIndex值<br />
     *          openTrigger==> 触发显示Tip的时间,一般为mouseover或click<br />
     *          closeTrigger==> //关闭事件，鼠标移开关闭<br />
     *          eventTarget==> 触发tip显示的源<br />
     *          tipContent==> tip存放的属性,默认为title<br />
     *          isAjax==> 是否用ajax方式获取tip中的内容<br />
     *          showType==> tip显示方式,提供多种展示方式，show,slideDown,fideIn<br />
     *          showSpeed==>显示速度,单位为毫秒<br />
     *          ajax, ==>当isAjax设置为ture时设置ajax获取数据的方式
     */
    giant.ui.tip.defaults = {
        width: 275,                 // Tip 的宽度
        height: 85,                 // Tip的高度
        tipId: "#giant-ui-tip",
        zIndex: 999,
        openTrigger: "mouseover",       //tip出现的事件。一般为mouseover或click
        closeTrigger: "mouseout",   //关闭事件，鼠标移开关闭
        eventTarget: ".tip",
        tipContent: "title",
        isAjax: false,
        position: "mouse",          
        showType: "show",           //提供多种展示方式，show,slideDown,fideIn
        showSpeed: 0,               //若为slideDown和fideIn,则可定制出现速度
        ajax: {
            type: "get",
            url: "",
            data: "",
            loadText: "数据加载中..."
        }
    };
})(jQuery);
